package cn.liutao.dish.api;

import cn.liutao.common.entity.Validation;
import cn.liutao.common.utils.ServletUtils;
import cn.liutao.common.utils.ValidUtil;
import cn.liutao.dish.entity.po.DiningTable;
import cn.liutao.dish.service.DiningTableService;
import com.mybatisflex.core.paginate.Page;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author liutao
 * @Description 餐桌api
 * @date 2023/11/1 14:41
 * @Version 1.0
 */
@WebServlet("/diningTable")
public class DiningTableServlet extends HttpServlet {
    private DiningTableService diningTableService;
    @Override
    public void init() throws ServletException {
        diningTableService = DiningTableService.getInstance();
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp){
        DiningTable queryParam = ServletUtils.getQueryParam(req, DiningTable.class);
        Page<DiningTable> page = diningTableService.selectList(queryParam);
        ServletUtils.ok(resp,page);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp){
        DiningTable addParam = ServletUtils.getBodyParam(req, DiningTable.class);
        ValidUtil.validated(addParam,Validation.AddGroup.class);
        diningTableService.insert(addParam);
        ServletUtils.ok(resp,"新增成功");
    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp){
        DiningTable editParam = ServletUtils.getBodyParam(req, DiningTable.class);
        ValidUtil.validated(editParam,Validation.EditGroup.class);
        diningTableService.updateById(editParam);
        ServletUtils.ok(resp,"修改成功");
    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp){
        List<String> ids = ServletUtils.getQueryParams(req, "id");
        for (String id : ids) {
            diningTableService.removeById(Integer.valueOf(id));
        }
        ServletUtils.ok(resp,"删除成功");
    }
}
